package sapling.api;

import io.swagger.annotations.ApiOperation;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sapling.model.Tree;
import sapling.service.TreeService;
import sapling.utils.Msg;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * Created by mac on 17/7/11.
 */
@RestController
@RequestMapping("/api/audit")
public class AuditApi {
    @Autowired
    TreeService treeService;

    @ApiOperation(value = "出库审核", notes = "出库苗木的treeids无论多个或一个在id后面加上分号，price出库价格，location去向")
    @PostMapping("/out/warehouse")
    public Msg<String> outwarehouse(@RequestParam String treeids,@RequestParam(required = false) BigDecimal price,@RequestParam(required = false) String location){
       int number=0;
        if(!TextUtils.isEmpty(treeids)) {
            String res = treeids.substring(0, treeids.length() - 1);
            for(String treeid:res.split(";")){
                long id = Long.parseLong(treeid);
                treeService.appoutwarehouse(Tree.TYPE_TREE_OUT,id,(Objects.isNull(price)?new BigDecimal("-1"):price),location);
                ++number;
            }

        }
        return Msg.newMsg("成功出库"+number+"棵");
    }

    @ApiOperation(value = "死苗确认", notes = "苗木的treeids无论多个或一个在id后面加上分号")
    @PostMapping("/die/confirm")
    public Msg<String> dieConfirm(@RequestParam String treeids){
        int number=0;
        if(!TextUtils.isEmpty(treeids)) {
            String res = treeids.substring(0, treeids.length() - 1);
            for(String treeid:res.split(";")){
                long id = Long.parseLong(treeid);
                treeService.appdieConfirm(Tree.TYPE_TREE_DIE,id);
                ++number;
            }
        }
        return Msg.newMsg("确认"+number+"棵");
    }
    @PostMapping("/die/confirm2")
    public Msg<String> dieConfirm2(@RequestParam String treeids){
        int number=0;
        if(!TextUtils.isEmpty(treeids)) {
            String res = treeids.substring(0, treeids.length() - 1);
            for(String treeid:res.split(";")){
                long id = Long.parseLong(treeid);
                treeService.appdieConfirm2(Tree.TYPE_TREE_DIE,id);
                ++number;
            }
        }
        return Msg.newMsg("确认"+number+"棵");
    }


}
